package com.example.sjly.sqliteroom;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.example.sjly.ui.login_register.register;
import com.example.sjly.ui.scheduling.room.TOrderDAO;
import com.example.sjly.ui.scheduling.room.TOrderIn;
import com.example.sjly.ui.scheduling.room.TOrderInDAO;
import com.example.sjly.ui.scheduling.room.TOrderMess;

@Database(entities = {User.class}, version =2,exportSchema = false)
public abstract class AppRoomDatabase extends RoomDatabase {

    private static AppRoomDatabase instance;

    public abstract UserDao userdao();


    public static AppRoomDatabase getInstance(Context context) {
        if (instance == null) {
            synchronized (AppRoomDatabase.class) {
                if (instance == null) {
                    instance = Room.databaseBuilder(context.getApplicationContext(), AppRoomDatabase.class, "room.db")
                            .fallbackToDestructiveMigration()//数据库更新时删除数据重新创建
                            .addMigrations(MIGRATION_1_2)
                            .build();
                }
            }
        }
        return instance;
    }

    static final Migration MIGRATION_1_2 = new Migration(1,2) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            //将数据表device创建出来
            database.execSQL("CREATE TABLE 'User' ('id'  long NOT NULL,'name' String,'username' String,'password' String," +
                    "'mail' String,'age' INT,'phone' String,'school' String,'region' String,'Sex' String,PRIMARY KEY ('id')) ");

     }
    };


}


